package org.erp.admins.controller;

import org.erp.admins.infrastructure.convert.TenantUserConvert;
import org.erp.admins.domain.bo.TenantUserBo;
import org.erp.admins.domain.dto.TenantUserDto;
import org.erp.admins.domain.vo.TenantUserVo;
import org.erp.admins.service.TenantUserService;
import org.erp.common.page.DataPageResp;
import org.erp.common.page.PageQueryModel;
import org.erp.common.domain.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static org.erp.common.domain.Result.success;

/**
 * 咸鱼店铺用户
 */
@RestController
@RequestMapping("/tenant/user")
public class TenantUserController {
    @Resource
    private TenantUserService tenantUserService;

    @Resource
    private TenantUserConvert tenantUserConvert;
    /**
     * 分页查询
     */
    @GetMapping("/getListByPage")
    public Result<DataPageResp<TenantUserVo>> getListByPage(TenantUserBo bo) {
        TenantUserDto dto = tenantUserConvert.boConvertDto(bo);
        PageQueryModel<TenantUserVo> qm = tenantUserService.getListByPage(dto);
        return success(DataPageResp.of(qm.getTotal(), qm.getDataList()));
    }
    /**
     * 查询列表
     */
    @GetMapping("/getList")
    public Result<List<TenantUserVo>> getList(TenantUserBo bo) {
        TenantUserDto dto = tenantUserConvert.boConvertDto(bo);
        return Result.success(tenantUserService.getTenantUserVoList(dto));
    }
    /**
     * 查询详情
     */
    @GetMapping("{id}")
    public Result<TenantUserVo> selectOne(@PathVariable Long id) {
        return Result.success(tenantUserService.getTenantUserVoById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result<Boolean> insert(@RequestBody TenantUserDto dto) {
        boolean result = tenantUserService.addTenantUser(dto);
        return result ? Result.success("新增成功") : Result.error("新增失败");
    }
    /**
     * 修改
     */
    @PostMapping("/upd")
    public Result<Boolean> update(@RequestBody TenantUserDto dto) {
        boolean result = tenantUserService.updTenantUser(dto);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }
    /**
     * 删除
     */
    @PostMapping("/del")
    public Result<Boolean> delete(@RequestBody TenantUserBo bo) {
        boolean result = tenantUserService.removeById(bo.getId());
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }
}

